<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Utils\Server\WeChatServer;
use App\Utils\Server\WxAmpServer;
use Illuminate\Http\Request;

/**
 * 用户微信服务配置
 * Class WxController
 * @package App\Http\Controllers\Api
 */
class WxController extends Controller
{

    /**
     * 微信公众号服务器消息校验
     * @param Request $request
     * @return mixed|string|void
     */
    public function getWeChatMsg(Request $request)
    {
        if (!$request->isMethod('post')) {
            /**
             * 验证消息来源于微信
             */
            $signature = $request->signature ?? '';
            $timestamp = $request->timestamp ?? '';
            $nonce     = $request->nonce ?? '';

            if (!WeChatServer::getInstance()->checkSignature($signature, $timestamp, $nonce)) return 'false';
            return $request->echostr ?? '';
        }
    }

    /**
     * 微信小程序服务器消息校验
     * @param Request $request
     * @return mixed|string|void
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
     */
    public function getAmpMsg(Request $request)
    {
        if (!$request->isMethod('post')) {
            /**
             * 验证消息来源于微信
             */
            $signature = $request->signature ?? '';
            $timestamp = $request->timestamp ?? '';
            $nonce     = $request->nonce ?? '';

            if (!WxAmpServer::getInstance()->checkSignature($signature, $timestamp, $nonce)) return 'false';
            return $request->echostr ?? '';
        }
    }
}